<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in categoryList" :key="item.id" class="f-item b-b" :class="{active: item.id === currentId}" @click="tabtap(item)">
				{{item.name}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
			<image class="image" :src="catepic"></image>
			<text class="txt">{{catename}} {{i18n.category.category}}</text>
			<!-- <view v-for="item in slist" :key="item.id" class="s-list" :id="'main-'+item.id"> -->
				<!-- <text class="s-item">{{item.name}}</text> -->
				<view class="t-list">
					<view  class="t-item" v-for="titem in currentSubCategory" :key="titem.id"  @tap="openinfo" :data-cateid="titem.id">
						<image :src="titem.picUrl"></image>
						<text>{{titem.name}}</text>
					</view>
				</view>
			<!-- </view> -->
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
				categoryList:[],
				catepic:'',
				catename:'',
				currentSubCategory:[],
			}
		},
		//computed监听
		computed:{
		   i18n() {  
		     return this.$i18nMsg()  
		   }
		},
		onLoad(){
			let title=this.$i18nMsg().pages.category;
			uni.setNavigationBarTitle({
				title
			});
			
			uni.setTabBarItem({
			  index: 1,
			  text: this.$i18nMsg().pages.tbarcate,
			})
			uni.showLoading({
				title: this.$i18nMsg().category.loading,
				mask: false
			});
			//this.loadData();
			this.loadcate();
			
			
		},
		methods: {
			// async loadData(){
			// 	let list = await this.$api.json('cateList');
			// 	list.forEach(item=>{
			// 		if(!item.pid){
			// 			this.flist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
			// 		}else if(!item.picture){
			// 			this.slist.push(item); //没有图的是2级分类
			// 		}else{
			// 			this.tlist.push(item); //3级分类
			// 		}
			// 	}) 
			// },
			async loadcate(e){
				
				uni.request({
					url: this.apiRoot+'catalog/index',
					method: 'GET',
					data: {},
					success: res => {
						this.categoryList=res.data.data.categoryList;
						let cateid = getApp().globalData.cateId;
						if(cateid>0){
							this.cateselect();
						}else{
						this.catepic = res.data.data.currentCategory.picUrl;
						this.catename = res.data.data.currentCategory.name;
						this.currentSubCategory = res.data.data.currentSubCategory;
						}
						uni.hideLoading();
					},
					fail: () => {},
					complete: () => {}
				});
			},
			// 
			cateselect(){
				
				let cateid = getApp().globalData.cateId;
					
					uni.request({
						url: this.apiRoot+'catalog/current?id='+cateid,
						method: 'GET',
						data: {},
						success: res => {
							this.currentSubCategory = res.data.data.currentSubCategory;
							this.catepic=res.data.data.currentCategory.picUrl;
							this.catename = res.data.data.currentCategory.name;
						},
						fail: () => {},
						complete: () => {}
					});
				
			},
			//一级分类点击
			tabtap(item){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				
				this.currentId = item.id;
				// let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				// this.tabScrollTop = this.slist[index].top;
				uni.request({
					url: this.apiRoot+'catalog/current?id='+item.id,
					method: 'GET',
					data: {},
					success: res => {
						this.catepic = res.data.data.currentCategory.picUrl;
						this.catename = res.data.data.currentCategory.name;
						this.currentSubCategory = res.data.data.currentSubCategory;
					},
					fail: () => {},
					complete: () => {}
				});
				
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid){
				uni.navigateTo({
					url: `/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
				})
			},
			openinfo(e) {
				
				var cateid = e.currentTarget.dataset.cateid;
				uni.navigateTo({
					url: '../product/list?cateid='+cateid
				});
				/* uni.navigateTo({
					url: '',
					success: res => {},
					fail: () => {},
					complete: () => {}
				}); */
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #fff;
	}
	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $font-color-base;
		position: relative;
		&.active{
			color: $base-color;
			background: #f8f8f8;
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: $base-color;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside{
		
		  border-left: 1px solid #fafafa;
		  flex: 1;
		  height: 100%;
		 
		
		.image{
			position: absolute;
			top: 30rpx;
			left: 0;
			border-radius: 4rpx;
			height: 192rpx;
			width: 100%;
		}
	
		.txt {
		  position: absolute;
		  margin-top: 210upx;
		  top: 30rpx;
		  text-align: center;
		  color: #000000;
		  font-size: 30upx;
		  left: 0; 
		  width: 100%;
		}
	}
	.s-item{
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		color: $font-color-dark;
	}
	.t-list{
		display: flex;
		flex-wrap: wrap;
		margin-top: 295upx;
		width: 100%;
		background: #fff;
		padding-top: 12upx;
		&:after{
			content: '';
			flex: 99;
			height: 0;
		}
	}
	.t-item{
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 176upx;
		font-size: 26upx;
		color: #666;
		padding-bottom: 20upx;
		
		image{
			width: 140upx;
			height: 140upx;
		}
	}
	
</style>
